package ua.maxicash.dao;

import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ua.maxicash.vo.Employer;
import ua.maxicash.vo.Worker;

import java.util.List;

@Repository("employerDao")
public class EmployerDaoImpl implements EmployerDao {

    private static final Logger logger = Logger.getLogger(EmployerDaoImpl.class);

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    @Transactional
    public void addEmployer(Employer employer) {

        hibernateTemplate.saveOrUpdate(employer);

        logger.info("new employer has been successfully added : " + employer.toString());

    }

    @Override
    @Transactional
    public void updateEmployer(Employer employer) {

        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("from Employer where idUser = :idUser");
        query.setLong("idUser",employer.getIdUser());
        Employer aEmployer = (Employer)query.list().get(0);

        aEmployer.setJobTypes(employer.getJobTypes());
        aEmployer.setAbout(employer.getAbout());
        aEmployer.setCity(employer.getCity());
        aEmployer.setFirstName(employer.getFirstName());
        aEmployer.setLastName(employer.getLastName());
        aEmployer.setPhone(employer.getPhone());
        aEmployer.setIcq(employer.getIcq());
        //todo always true
        aEmployer.setEnabled(true);
        aEmployer.setSkype(employer.getSkype());
        aEmployer.setOrganization(employer.getOrganization());
        if (employer.getAvatar()!=null) {
            aEmployer.setAvatar(employer.getAvatar());
        }

        session.merge(aEmployer);

        logger.info("employer has been successfully updated : " + employer.toString());

    }

    @Override
    public List<Employer> listEmployer() {

        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Employer.class);
        List<Employer> list = criteria.list();
        session.close();

        return list;

    }

    @Override
    public Employer getEmployerByEmail(String email) {

        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Employer.class);
        criteria.add(Restrictions.eq("email", email).ignoreCase());
        Employer employer = (Employer) criteria.uniqueResult();
        session.close();

        return employer;
    }

    @Override
    public boolean emailIsFree(String email) {

        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Employer.class);
        criteria.add(Restrictions.eq("email", email).ignoreCase());
        Employer employer = (Employer) criteria.uniqueResult();
        session.close();


        return employer == null;
    }

    @Override
    @Transactional
    public Employer updatePassword(String email, String password) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Employer.class);
        criteria.add(Restrictions.eq("email", email).ignoreCase());
        Employer employer = (Employer) criteria.uniqueResult();
        employer.setPassword(password);
        session.saveOrUpdate(employer);
        session.close();
        return employer;
    }


    @Override
    public Employer getEmployerById(Long idEmployer) {
        DetachedCriteria crit = DetachedCriteria.forClass(Employer.class);
        crit.add(Restrictions.idEq(idEmployer));
        return (Employer) hibernateTemplate.findByCriteria(crit).get(0);
    }
}
